﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace ShadowEraDeckEditor
{
    public partial class FormMenu : Form
    {
        public FormMenu()
        {
            InitializeComponent();
        }
        //create variables for the "current form (next form)" and for all the Forms
        public int nextform { get; set; }
        public string currentdeck {get;set;}
        public int userlevel { get; set; }
        Settings Settings = new Settings();
        private FormDeckBuilder _decktab;
        private FormDevTools _devtab;
        private FormDeckHack _deckhacktab;
        private FormDeckHackLite _deckhacklite;
        private FormSecurity _securitytab;
        private FormOptions _optionstab;

        private FormUpdate _updatetab;


        private void FormMenu_Load(object sender, EventArgs e)
        {
            //set first Form to the Deck Builder
            currentdeck = "";
            userlevel = 1;
            nextform = 1;
            //new thread for checking updates
            Thread thread = new Thread(new ThreadStart(autoupdatecheck));
            thread.Start();
            while (nextform != 5)
            {
                //hide the main menu, buffer the current form, and launch current form
                this.Hide();
                int nextformbuffer = nextform;
                switch (nextform)
                {
                    case 1:
                        _decktab = new FormDeckBuilder();
                        //_decktab.Closed += (s, a) => { this.Show(); };
                        _decktab.ShowDialog(this);
                        break;
                    case 2:
                        _devtab = new FormDevTools();
                        //_updatetab.Closed += (s, a) => { this.Show(); };
                        _devtab.ShowDialog(this);
                        break;
                    case 3:
                        _deckhacklite = new FormDeckHackLite();
                        _deckhacklite.ShowDialog(this);
                        break;
                    case 4:
                        _deckhacktab = new FormDeckHack();
                        _deckhacktab.ShowDialog(this);
                        break;
                    case 6:
                        _securitytab = new FormSecurity();
                        _securitytab.ShowDialog(this);
                        break;
                    case 7:
                        _optionstab = new FormOptions();
                        _optionstab.ShowDialog(this);
                        break;
                    case 8:
                        _updatetab = new FormUpdate();
                        _updatetab.ShowDialog(this);
                        break;

                }
                //if the previous form exited without passing a nextform, exit the program
                if (nextform == nextformbuffer)
                    nextform = 5;
            }
            Application.Exit();
        }
        void autoupdatecheck()
        {
            if (Settings.autoUpdate == true && Settings.datVersion != "exit")
            {
                MemoryStream inputStream = new MemoryStream();
                WebClient client = new WebClient();
                DialogResult result;
                string appVersionURL = "http://dl.dropbox.com/u/30391649/se%20deck%20editor/versions.ini";
                string updateAppVer;
                string updateDatVer;
                try
                {
                    inputStream = new MemoryStream(client.DownloadData(appVersionURL));
                    inputStream.Position = 0;
                    using (StreamReader reader = new StreamReader(inputStream))
                    {
                        updateAppVer = reader.ReadLine();
                        updateDatVer = reader.ReadLine();
                    }
                    client.Dispose();
                    if ((updateAppVer != Settings.appVersion.ToString()) || (updateDatVer != Settings.datVersion.ToString()))
                    {
                        result = MessageBox.Show("Would you like to download them now?", "Updates are available.", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            var updateForm = new FormUpdate();
                            updateForm.ShowDialog();
                            if (Settings.appVersion == "exit")
                                Application.Exit();
                        }

                    }

                }
                catch
                {
                    MessageBox.Show("something broke");
                }
            }
            
        }

    }
}
